<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <h2>管理员登录</h2>
        </div>
      </template>
      
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="80px"
        @keyup.enter="handleLogin"
      >
        <el-form-item label="账号" prop="admin_id">
          <el-input
            v-model="form.admin_id"
            placeholder="请输入管理员账号"
            :prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="submit-btn"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useAdminStore } from '@/store/admin'
import { adminApi } from '@/api/admin'

const router = useRouter()
const route = useRoute()
const adminStore = useAdminStore()
const formRef = ref<FormInstance>()
const loading = ref(false)

const form = reactive({
  admin_id: '',
  password: '',
})

const rules: FormRules = {
  admin_id: [
    { required: true, message: '请输入管理员账号', trigger: 'blur' },
    { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!formRef.value) return
  try {
    await formRef.value.validate()
    loading.value = true
    // 正确传参方式
    const res = await adminApi.login({
      admin_id: parseInt(form.admin_id),
      password: form.password
    })
    if (res.success) {
      ElMessage.success('登录成功')
      // 保存token
      localStorage.setItem('admin_token', res.data.token)
      // 跳转到后台首页或重定向
      const redirect = route.query.redirect as string
      router.push(redirect || '/admin/dashboard')
    } else {
      ElMessage.error(res?.message || '登录失败')
    }
  } catch (error: any) {
    ElMessage.error(error.message || '登录失败')
    console.error('登录错误:', error)
  } finally {
    loading.value = false
  }
}

</script>

<style scoped lang="scss">
@use '@/assets/styles/variables.scss' as *;

.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: $background-color;
  
  .login-card {
    width: 400px;
    
    .card-header {
      text-align: center;
      h2 {
        margin: 0;
        color: $text-primary;
      }
    }
  }
}
</style>